<template>
    <div class="login_wrapper">
        <div class="login_box">
            <!--头像-->
            <div class="head_content">
                <img src="../assets/logo.png" alt="">
            </div>
            <!--登录内容-->
            <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    // 表单的数据绑定
    return {
      loginForm: {
        username: '',
        password: ''
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '输入3到6个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '输入6到12个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.loginRef.resetFields()
    },
    login () {
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('fail')
        this.$message.success('success')
        console.log(res)
        // 保存token
        window.sessionStorage.setItem('token', res.data.token)
        // 跳转到主页面
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_wrapper{
    background-color: skyblue;
    height: 100%;
    .login_box {
        width: 450px;
        height: 260px;
        background-color: #fff;
        position: absolute;
        left:50%;
        top:50%;
        transform: translate(-50%,-50%);
        border-radius: 3px;
        .head_content {
            width: 130px;
            height: 130px;
            border-radius: 50%;
            padding: 10px;
            box-sizing: border-box;
            position: absolute;
            background-color: #fff;
            transform: translate(-50%,-50%);
            left: 50%;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }

        }
        .login_form {
            width:100%;
            padding: 0 20px;
            box-sizing: border-box;
            position: absolute;
            bottom: 0px;
            .btns {
                display: flex;
                justify-content: flex-end;
            }
        }
    }
}
</style>
